package com.business.music;

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.beans.music.MusicInfo;
import com.config.SystemGlobals;
import com.sql.DBFactory;
import com.util.DeleteFile;

public class MusicInfoBus {
	private Connection conn = null;

	private Statement stmt = null;



	private static final Log log = LogFactory.getLog(MusicInfoBus.class);

	public void add(MusicInfo mi) {
		String musicName = mi.getMusicName();
		String musicMemo = mi.getMusicMemo();
		String musicUrl = mi.getMusicUrl();
		String musictypeId = mi.getMusictypeId();
		String musicNo = mi.getMusicNo();
		String viewCount = mi.getViewCount();
		String isShow = mi.getIsShow();
		String addTime = mi.getAddTime();
		String username = mi.getUsername();
		String className = mi.getClassName();
		String singer = mi.getSinger();
		String lrcUrl = mi.getLrcUrl();
		String sql = "insert into music_info(music_name,music_no,username,add_time,"
				+ "view_count,musictypeid,music_url,music_memo,isShow,class_name,singer,lrc_url) values('";
		sql += musicName + "','" + musicNo + "','" + username + "','" + addTime
				+ "'," + viewCount + "," + musictypeId + ",'";
		sql += musicUrl + "','" + musicMemo + "'," + isShow + ",'" + className
				+ "','" + singer + "','" + lrcUrl + "')";
		try {
			conn = DBFactory.getConnection();
			stmt = conn.createStatement();
			stmt.executeUpdate(sql);
		} catch (Exception e) {
			log.error("the insert musicinfo exception:" + e.getMessage());
			log.error("the insert musicinfo sql is: " + sql);
		} finally {
			try {
				stmt.close();
				conn.close();
			} catch (SQLException sqle) {
				log.error(sqle.toString());
			}
		}

	}

	public void update(MusicInfo mi, String musicNo) {
		String musicName = mi.getMusicName();
		String musicMemo = mi.getMusicMemo();
		String musicUrl = mi.getMusicUrl();
		String musictypeId = mi.getMusictypeId();
		String className = mi.getClassName();
		String singer = mi.getSinger();
		String lrcUrl = mi.getLrcUrl();
		String viewCount = mi.getViewCount();
		String isShow = mi.getIsShow();
		String username = mi.getUsername();
		String sql = "update music_info set music_name='" + musicName
				+ "',username='" + username + "',view_count=" + viewCount
				+ ",musictypeid=" + musictypeId + ",class_name='" + className
				+ "',singer='" + singer + "',music_url='" + musicUrl
				+ "',lrc_url='" + lrcUrl + "',music_memo='" + musicMemo
				+ "',isShow=" + isShow + " where music_no='" + musicNo + "'";
		try {
			conn = DBFactory.getConnection();
			stmt = conn.createStatement();
			stmt.executeUpdate(sql);
		} catch (Exception e) {
			log.error("the update MusicInfo exception:" + e.getMessage());
			log.error("the update MusicInfo sql is: " + sql);
		} finally {
			try {
				stmt.close();
				conn.close();
			} catch (SQLException sqle) {
				log.error(sqle.toString());
			}
		}

	}

	public void del(String musicNo) {
		String sql = "delete from music_info where music_no='" + musicNo + "'";
		try {
			MusicListBus plb = new MusicListBus();
			MusicInfo mi = plb.getMusicByNo(musicNo);
			String musicUrl = mi.getMusicUrl();

			DeleteFile.delete(SystemGlobals.getValue("web_root") + musicUrl);
			if (!mi.getLrcUrl().equals("")) {
				DeleteFile.delete(SystemGlobals.getValue("web_root")
						+ mi.getLrcUrl());
				// System.out.println(sql);

			}
			conn = DBFactory.getConnection();
			stmt = conn.createStatement();
			stmt.executeUpdate(sql);

		} catch (Exception e) {
			log.error("the delete MusicInfo exception:" + e.getMessage());
			log.error("the delete MusicInfo sql is: " + sql);
		} finally {
			try {
				stmt.close();
				conn.close();
			} catch (SQLException sqle) {
				log.error(sqle.toString());
			}
		}

	}
}
